package top.jiangqiang.qyblog.common.exception;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author jiangqiang
 * @date 2022-07-30
 */
@Controller
@Slf4j
//@RequestMapping("/error")
//@RequestMapping("${server.error.path:${error.path:/error}}")
public class ErrorController extends BasicErrorController {

    public ErrorController(ErrorAttributes errorAttributes) {
        super(errorAttributes, new ErrorProperties());
    }

//    /**
//     * 响应错误页面时会走到这里
//     *
//     * @param request
//     * @param response
//     * @return
//     */
//    @RequestMapping(produces = MediaType.TEXT_HTML_VALUE)
//    @Override
//    public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
//        HttpStatus status = getStatus(request);
//        Map<String, Object> model = getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.TEXT_HTML));
//        response.setStatus(status.value());
////        ModelAndView modelAndView = new ModelAndView(new FastJsonJsonView());
//        ModelAndView modelAndView = resolveErrorView(request, response, status, model);
//        String path = (String) model.get("path");
//        if (StrUtil.isNotBlank(path)) {
//            //path某些字符是编码过的
//            model.put("path", URLUtil.decode(path));
//        }
//        log.info("[errorHtml]:{}", model);
//        String key = cache.get(CacheConstants.CACHE_CMS_CMS_SERVICE + "getKey");
//        if (key == null) {
//            key = "default";
//        }
//        return (modelAndView != null) ? modelAndView : new ModelAndView(key + CmsConstants.serverErrorTemplatePath, model);
//    }

    /**
     * GlobalExceptionHandler中没有处理的异常并且响应类型不是页面时才会走到这里
     *
     * @param request
     * @return
     */
    @Override
    public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
        final Map<String, Object> errorAttributes = getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL));
        final HttpStatusCode status = getStatus(request);
        Map<String, Object> result = new LinkedHashMap<>();
        result.put("code", errorAttributes.get("status"));
        result.put("message", errorAttributes.get("error"));
        Map<String, Object> data = new HashMap<>();
        data.put("path", errorAttributes.get("path"));
        data.put("timestamp", errorAttributes.get("timestamp"));
        result.put("data", data);
        log.error("[error]:{}", result.toString());
        return new ResponseEntity<>(
                result,
                status
        );
    }
}
